package Management;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

	public class Login extends JFrame {

		JButton blogin = new JButton("Login");
		JButton clogin = new JButton("Cancel");
		JPanel panel = new JPanel();
		JTextField txuser = new JTextField(15);
		JPasswordField pass = new JPasswordField(15);
		JLabel usernamelabel = new JLabel("User Name: ");
		JLabel passwordlabel = new JLabel("Password: ");
		JLabel titlelabel = new JLabel(" Login ");

		Login(){
			
			super("Login Authentication");
			setSize(300,200);
			setLocation(500,280);
			panel.setLayout (null); 
			usernamelabel.setLayout(null);
			passwordlabel.setLayout(null);

			titlelabel.setLocation(0,0);
			titlelabel.setSize(220, 30);
			titlelabel.setHorizontalAlignment(0);
			
			txuser.setBounds(100,30,150,20);
			usernamelabel.setBounds(20,30,150,20);
			pass.setBounds(100,65,150,20);
			passwordlabel.setBounds(20,65,150,20);
			blogin.setBounds(50,100,80,20);
			clogin.setBounds(160, 100, 80, 20);
			
			panel.add(titlelabel);
			panel.add(blogin);
			panel.add(clogin);
			panel.add(usernamelabel);
			panel.add(txuser);
			panel.add(passwordlabel);
			panel.add(pass);

			getContentPane().add(panel);
			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			setVisible(true);
			actionlogin();
}
 
		public void actionlogin(){
			blogin.addActionListener(new ActionListener() {
		
				public void actionPerformed(ActionEvent ae) {
					String puname = txuser.getText();
					String ppaswd = pass.getText();
					
					if(puname.equals("") && ppaswd.equals("")) {
						DivisionFrame compframe =new DivisionFrame();
						compframe.setVisible(true);
						dispose();
					} else {

						JOptionPane.showMessageDialog(null,"Wrong Password / Username");
						txuser.setText("");
						pass.setText("");
						txuser.requestFocus();
					}

				}
				
			});
			
			clogin.addActionListener(new ActionListener() {
				
				public void actionPerformed(ActionEvent ae) {
					
					System.exit(0);

				}
				
			});
			
			pass.addKeyListener(new KeyListener(){

		                public void keyPressed(KeyEvent e){
		                	
		                	String puname = txuser.getText();
							String ppaswd = pass.getText();
		                	
		                    if(e.getKeyChar() == KeyEvent.VK_ENTER)if(puname.equals("") && ppaswd.equals("")) {
								DivisionFrame compframe =new DivisionFrame();
								compframe.setVisible(true);
								dispose();
							} else {

								JOptionPane.showMessageDialog(null,"Wrong Password / Username");
								txuser.setText("");
								pass.setText("");
								txuser.requestFocus();
								
							}
		      
		                }

						@Override
						public void keyReleased(KeyEvent arg0) {
							// TODO Auto-generated method stub
							
						}

						@Override
						public void keyTyped(KeyEvent arg0) {
							// TODO Auto-generated method stub
							
						}
		            }
		        );
			
			txuser.addKeyListener(new KeyListener(){

                public void keyPressed(KeyEvent e){
                	
                	String puname = txuser.getText();
					String ppaswd = pass.getText();
                	
                    if(e.getKeyChar() == KeyEvent.VK_ENTER)if(puname.equals("") && ppaswd.equals("")) {
						DivisionFrame compframe =new DivisionFrame();
						compframe.setVisible(true);
						dispose();
					} else {

						JOptionPane.showMessageDialog(null,"Wrong Password / Username");
						txuser.setText("");
						pass.setText("");
						txuser.requestFocus();
						
					}
              
                }

				@Override
				public void keyReleased(KeyEvent arg0) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void keyTyped(KeyEvent arg0) {
					// TODO Auto-generated method stub
					
				}
            }
        );

		
		}
		public static void main(String[] args) {

			Login frameTable = new Login();
			frameTable.setLocationRelativeTo(null);
		}
}
